﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.OleDb;

namespace 自消费
{
    [Serializable]
    public class deviceData
    {

        public const string BAUDRATEField = "@baudrate";

        public const string COMMPORTField = "@commport";

        public const string ENABLEDField = "@enabled";

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string IMEIField = "@imei";

        public const string MAXCOUNT_SMS_SENDField = "@maxcount_sms_send";

        public const string SEQNUMField = "@seqnum";

        public const string TYPEField = "@type";

        private Int32 _baudrate;

        private Int32 _commport;

        private Boolean _enabled;

        private Int32 _ID;

        private String _imei;

        private Int32 _maxcount_sms_send;

        private Int32 _seqnum;

        private Int32 _type;

        public Int32 baudrate
        {
            get { return _baudrate; }
            set { _baudrate = value; }
        }
        public Int32 commport
        {
            get { return _commport; }
            set { _commport = value; }
        }
        public Boolean enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        public Int32 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String imei
        {
            get { return _imei; }
            set { _imei = value; }
        }
        public Int32 maxcount_sms_send
        {
            get { return _maxcount_sms_send; }
            set { _maxcount_sms_send = value; }
        }
        public Int32 seqnum
        {
            get { return _seqnum; }
            set { _seqnum = value; }
        }
        public Int32 type
        {
            get { return _type; }
            set { _type = value; }
        }
        public deviceData()
        {
            _baudrate = 0;
            _commport = 0;
            _enabled = true;
            _ID = 0;
            _imei = "123132";
            _maxcount_sms_send = -1;
            _seqnum = 0;
            _type = 0;
        }
    }

    public class deviceDataFactory
    {
        public static deviceData Construct(IDataReader reader)
        {
            deviceData data = new deviceData();

            try { data.baudrate = Convert.ToInt32(reader["baudrate"]); }
            catch { }

            try { data.commport = Convert.ToInt32(reader["commport"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(reader["enabled"]); }
            catch { }

            try { data.ID = Convert.ToInt32(reader["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(reader["imei"]); }
            catch { }

            try { data.maxcount_sms_send = Convert.ToInt32(reader["maxcount_sms_send"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(reader["seqnum"]); }
            catch { }

            try { data.type = Convert.ToInt32(reader["type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Int32 GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dt.Rows.Count < rowIndex + 1)

                return 0;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return 0;

            try { PrimaryKey = Convert.ToInt32(dr[deviceData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static deviceData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static deviceData Construct(DataTable dt, int rowIndex)
        {
            deviceData data = new deviceData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.baudrate = Convert.ToInt32(dr["baudrate"]); }
            catch { }

            try { data.commport = Convert.ToInt32(dr["commport"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(dr["enabled"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.maxcount_sms_send = Convert.ToInt32(dr["maxcount_sms_send"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(dr["seqnum"]); }
            catch { }

            try { data.type = Convert.ToInt32(dr["type"]); }
            catch { }

            return data;
        }
        public static deviceData Construct(DataGridView dgv, int rowIndex)
        {
            deviceData data = new deviceData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.baudrate = Convert.ToInt32(dr["baudrate"]); }
            catch { }

            try { data.commport = Convert.ToInt32(dr["commport"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(dr["enabled"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.maxcount_sms_send = Convert.ToInt32(dr["maxcount_sms_send"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(dr["seqnum"]); }
            catch { }

            try { data.type = Convert.ToInt32(dr["type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dgv.Rows.Count < rowIndex + 1)

                return 0;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return 0;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == deviceData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToInt32(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
        public static deviceData Construct(DataRow dr)
        {
            deviceData data = new deviceData();

            try { data.baudrate = Convert.ToInt32(dr["baudrate"]); }
            catch { }

            try { data.commport = Convert.ToInt32(dr["commport"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(dr["enabled"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.maxcount_sms_send = Convert.ToInt32(dr["maxcount_sms_send"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(dr["seqnum"]); }
            catch { }

            try { data.type = Convert.ToInt32(dr["type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataRow dr)
        {
            Int32 PrimaryKey = 0;

            try { PrimaryKey = Convert.ToInt32(dr[deviceData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class deviceDataDBOption
    {
        public static int Insert(deviceData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into device (");
            sql.Append("baudrate, ");
            sql.Append("commport, ");
            sql.Append("enabled, ");
            sql.Append("imei, ");
            sql.Append("maxcount_sms_send, ");
            sql.Append("seqnum, ");
            sql.Append("type");
            sql.Append(") values (");
            sql.Append("");
            sql.Append(d.baudrate.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.commport.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.enabled.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.imei.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.maxcount_sms_send.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.seqnum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.type.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(deviceData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update device set ");
            sql.Append("baudrate=");
            sql.Append("");
            sql.Append(d.baudrate.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("commport=");
            sql.Append("");
            sql.Append(d.commport.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("enabled=");
            sql.Append("");
            sql.Append(d.enabled.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("imei=");
            sql.Append("'");
            sql.Append(d.imei.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("maxcount_sms_send=");
            sql.Append("");
            sql.Append(d.maxcount_sms_send.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("seqnum=");
            sql.Append("");
            sql.Append(d.seqnum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("type=");
            sql.Append("");
            sql.Append(d.type.ToString());
            sql.Append("");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(deviceData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from device ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect(deviceData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from device ");
            return WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect(deviceData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from device ");
            return WFNetLib.ADO.AccessOP.DataSetSQL(sql.ToString());
        }
        public static OleDbDataReader ReaderSelect(deviceData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from device ");
            return WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql.ToString());
        }
    }
}